//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 15
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace GropuedFindItemUsingProxy
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            GroupedFindItemUsingProxy(binding);
        }

        /// <summary>
        /// Grouped FindItem call using the proxy (Listing 15-17)
        /// </summary>
        /// <param name="binding">Binding used for the call</param>
        ///
        static void GroupedFindItemUsingProxy(ExchangeServiceBinding binding)
        {
            FindItemType request = new FindItemType();
            // Set up our grouping
            //
            GroupByType grouping = new GroupByType();
            grouping.AggregateOn = new AggregateOnType();
            grouping.AggregateOn.Item = new
            PathToUnindexedFieldType(
            UnindexedFieldURIType.itemDateTimeCreated);
            grouping.AggregateOn.Aggregate = AggregateType.Maximum;
            grouping.Item = new
            PathToUnindexedFieldType(UnindexedFieldURIType.itemSize);
            grouping.Order = SortDirectionType.Ascending;
            // How intuitive! Well, of course Item1 would be the grouping type. What
            // else could it be? :)
            request.Item1 = grouping;
            request.ItemShape = new ItemResponseShapeType();
            request.ItemShape.BaseShape = DefaultShapeNamesType.IdOnly;
            PathToUnindexedFieldType subjectPath = new
            PathToUnindexedFieldType(UnindexedFieldURIType.itemSubject);
            PathToUnindexedFieldType sizePath = new
            PathToUnindexedFieldType(UnindexedFieldURIType.itemSize);

            request.ItemShape.AdditionalProperties = new
            BasePathToElementType[] { subjectPath, sizePath };
            request.Traversal = ItemQueryTraversalType.Shallow;
            DistinguishedFolderIdType drafts = new DistinguishedFolderIdType();
            drafts.Id = DistinguishedFolderIdNameType.drafts;
            request.ParentFolderIds = new BaseFolderIdType[] { drafts };
            // issue our request
            FindItemResponseType response = binding.FindItem(request);
            FindItemResponseMessageType responseMessage =
            response.ResponseMessages.Items[0] as FindItemResponseMessageType;
            // Since we did a grouped find item, we can expect to get our grouping
            // response
            if (responseMessage.ResponseCode == ResponseCodeType.NoError)
            {
                // use our new helper method to get the grouped results out
                GroupedItemsType[] groups =
                responseMessage.RootFolder.GetGroupedResults();
                foreach (GroupedItemsType group in groups)
                {
                    Console.WriteLine("Group ID: {0}", group.GroupIndex);
                    foreach (ItemType item in group.Items.Items)
                    {
                        Console.WriteLine(" Subject: {0}, Size: {1}",
                        item.Subject, item.Size);
                    }
                }
            }
        }
    }
}
